package com.chenfan.filestore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenfan.filestore.dto.BatchExportTemplateQueryDTO;
import com.chenfan.filestore.model.BatchExportTemplate;
import com.chenfan.filestore.repository.BatchExportTemplateMapper;
import com.chenfan.filestore.service.BatchExportTemplateService;
import com.chenfan.filestore.utils.LambdaQueryUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author K
 * @date 2023/4/26 5:35 PM
 */
@Service
public class BatchExportTemplateServiceImpl extends ServiceImpl<BatchExportTemplateMapper, BatchExportTemplate> implements BatchExportTemplateService {

    @Autowired
    private BatchExportTemplateMapper batchExportTemplateMapper;

    @Override
    public BatchExportTemplate findByTemplateCode(String templateCode) {
        LambdaQueryWrapper<BatchExportTemplate> lambdaQueryWrapper = LambdaQueryUtil.newLambdaQuery();
        lambdaQueryWrapper.eq(BatchExportTemplate::getTemplateCode, templateCode);
        return this.getOne(lambdaQueryWrapper);
    }

    @Override
    public Page<BatchExportTemplate> pageBatchExportTemplate(BatchExportTemplateQueryDTO query) {
        Page<BatchExportTemplate> page = new Page<>(query.getPageNum(), query.getPageSize());
        LambdaQueryWrapper<BatchExportTemplate> lambdaQueryWrapper = LambdaQueryUtil.newLambdaQuery();
        lambdaQueryWrapper.eq(Objects.nonNull(query.getSystemId()), BatchExportTemplate::getSystemId, query.getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(query.getSystemId()), BatchExportTemplate::getSystemId, query.getSystemId());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(query.getTemplateCode()), BatchExportTemplate::getTemplateCode, query.getTemplateCode());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(query.getTemplateName()), BatchExportTemplate::getTemplateName, query.getTemplateName());
        lambdaQueryWrapper.eq(Objects.nonNull(query.getStatus()), BatchExportTemplate::getStatus, query.getStatus());
        lambdaQueryWrapper.ge(Objects.nonNull(query.getUpdateDateStart()), BatchExportTemplate::getUpdateDate, query.getUpdateDateStart());
        lambdaQueryWrapper.le(Objects.nonNull(query.getUpdateDateEnd()), BatchExportTemplate::getUpdateDate, query.getUpdateDateEnd());
        lambdaQueryWrapper.orderByDesc(BatchExportTemplate::getUpdateDate);
        return this.page(page, lambdaQueryWrapper);
    }
}
